(function($){
	/**
	 * 检验ajax请求服务端的处理是否成功
	 * @param data
	 * @returns {boolean}
	 */
	$.ajaxCheck = function(data){
		if(1 == data.result){
			return true;
		}else{
			alert(data.msg);
			return false;
		}
	}


	$.fn.mytree = function(opts){
		var defs = {
			data: {
				simpleData : {
					enable : true,
					idKey : "id",
					pIdKey : "pid",
					rootPId : -1
				}
			},
			view: {
				dblClickExpand : false,
				selectedMulti : false
			},
			async: {
				enable: true,
				url: opts ? (opts.url||"channelTreeAll") : "channelTreeAll"
			},
			callback: {
				onAsyncSuccess: expandTreeAll
			},
			mine: {
				expandAll:  1,
				listChildren: -1,
				childrenId: "#cc"
			}
		};
		var setting = $.extend(defs, opts||{});
		var _mine = setting.mine;
		var tree = $.fn.zTree.init(this, setting);
		if(_mine.listChildren>0){
			tree.setting.callback.onClick = listChildren;
		}
		//TODO 觉得应该放在TreeNode里
		tree.getSiblings = getSiblings;
		tree.getAllParents = getAllParents;
		tree.getAllChildren = getAllChildren;
		tree.disposeBeforeCheck = disposeBeforeCheck;
		return tree;

		function listChildren(event, treeId, treeNode){
			$(_mine.childrenId).attr("src","channels/"+treeNode.id);
		}
		function expandTreeAll(){
			if(_mine.expandAll>0){
				tree.expandAll(true);
			}
		}
		function getSiblings(node){
			var p = node.getParentNode();
			if(!p){
				return [];
			}
			var p_children = p.children;
			var siblings = [];
			//从孩子里去除node
			for(var i=0; i<p_children.length; i++){
				if(p_children[i].id != node.id){
					siblings.push(p_children[i]);
				}
			}
			return siblings;
		}
		function getAllParents(node){
			var parents = [];
			var p = node;
			while(p=p.getParentNode()){
				parents.push(p);
			}
			return parents;
		}
		function getAllChildren(node){
			var children = [];
			getLevelOneChildren(node);
			return children;

			function getLevelOneChildren(node){
				var levelOneChildren = node.children;
				if(levelOneChildren){
					for(var i=0; i<levelOneChildren.length; i++){
						var child = levelOneChildren[i];
						children.push(child);
						getLevelOneChildren(child);
					}
				}
			}
		}
		function disposeBeforeCheck(treeId, treeNode){
			var checking = [];
			var unchecking = [];
			if(!treeNode.checked){
				//勾选
				//父节点勾选
				checking.push(treeNode);
				var p = treeNode;
				while(p=p.getParentNode()){
					//checking.push(p);
					if(!p.checked){
						checking.push(p);
					}else{
						break;
					}
				}
			}else{
				unchecking.push(treeNode);
				//取消勾选
				//子节点取消勾选，条件：该节点的全部子孙里状态为checked
				var children = getAllChildren(treeNode);
				for(var i=0; i<children.length; i++){
					if(children[i].checked){
						unchecking.push(children[i]);
					}
				}
				//父节点取消勾选，条件：父节点的所有孩子除了该孩子外都是unchecked状态
				dealSiblings(treeNode, unchecking);
			}
			var nodes = new Object();
			nodes.checking = checking;
			nodes.unchecking = unchecking;
			return nodes;
		}
		function dealSiblings(node, unchecking){
			var q = node;
			var q_siblings = getSiblings(q);
			var hasCheckedChild = false;
			for(var j=0; j<q_siblings.length; j++){
				if(q_siblings[j].checked){
					hasCheckedChild = true;
					break;
				}
			}
			var p = q.getParentNode();
			if(!p){
				return;
			}
			if(!hasCheckedChild){
				unchecking.push(p);
				dealSiblings(p, unchecking);
			}else{
				return;
			}
		}
	}
})(jQuery);